package io.kiki.sba.registry.common.model.elector;

import lombok.Getter;
import lombok.Setter;

import java.util.Date;


@Setter
@Getter
public class Lock {


    private long id;

    /**
     * local data center
     */
    private String dataCenter;


    private String lockName;

    /**
     * lock owner
     */
    private String lockOwner;

    /**
     * duration(millisecond)
     */
    private long duration;


    private Date gmtCreate;


    private Date gmtModified;

    /**
     * current timestamp in db
     */
    private Date gmtDbServerTime;

    private long gmtModifiedUnixNanos;

    private long term;

    private long termDuration;

    public Lock() {
    }

    public Lock(String dataCenter, String lockName, String lockOwner, long duration) {
        this.dataCenter = dataCenter;
        this.lockName = lockName;
        this.lockOwner = lockOwner;
        this.duration = duration;
    }


    public boolean expire() {

        return gmtDbServerTime.getTime() > gmtModified.getTime() + duration;
    }


    public long getGmtModifiedUnixMillis() {
        return gmtModifiedUnixNanos / 1000000;
    }
}
